﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary;
using CommonLibrary.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace BusinessLibrary
{
    /// <summary>
    /// RJ6902GX Hipot类
    /// </summary>
    public class RJ6902GX : IPlugin
    {
        /// <summary>
        /// 查询标准波形信息
        /// </summary>
        public static readonly byte QUERY_STANDARD_WAVE = 0x6B;
        /// <summary>
        /// 查询测试判定数据、结果
        /// </summary>
        public static readonly byte TEST_DATA_RESULT = 0x5B;
        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description => "RJ6902GX相关业务处理";
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        public string Excute(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            if (pluginParam != null
                && APPInstance.PLC != null
                && APPInstance.PLC.IsConnected)
            {
                string identity = pluginParam.Identity.ToUpper();
                switch (identity)
                {
                    case "RJ6902GX_PARAMETER": result = RJ6902GXParameter(pluginParam); break;
                    case "RJ6902GX_TEST": result = RJ6902GXTest(pluginParam); break;
                }
            }
            return result;
        }
        /// <summary>
        /// 返回一个窗口
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>

        public HWFrmWithTitle GetForm(PluginParamEntity pluginParam)
        {
            return null;
        }
        /// <summary>
        /// 返回一个 Bitmap
        /// </summary>
        /// <returns></returns>
        public Bitmap GetBitmap()
        {
            return null;
        }
        /// <summary>
        /// RJ6902GX_PARAMETER 参数查询
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        private string RJ6902GXParameter(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            string methodName = "Hipot参数查询";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            if (pluginParam != null && APPInstance.PLC != null && APPInstance.PLC.IsConnected)
            {
                var entity = CommonHelper.GetJson(pluginParam.ParamString, new RJ6902ExcuteEntity());
                try
                {
                    if (entity != null && entity.QueryPLCEntity != null)
                    {
                        entity.IsOK = false;
                        if (this.IsCorrectData(entity.Data))
                        {
                            byte cmd = entity.Data[5];
                            if (cmd == QUERY_STANDARD_WAVE)
                            {
                                byte[] dataBuffer = entity.Data.Skip(6).Take(entity.Data.Length - 8).ToArray();
                                try
                                {
                                    StructRJWaveData s = (StructRJWaveData)CommonHelper.BytesToStruct(dataBuffer, typeof(StructRJWaveData));
                                    entity.IsOK = true;
                                    #region dictionary
                                    Dictionary<string, string> dic = new Dictionary<string, string>();
                                    dic.Add("No", short.Parse(string.Join("", s.No.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    dic.Add("Name", Encoding.ASCII.GetString(s.Name));
                                    dic.Add("Voltage", short.Parse(string.Join("", s.Voltage.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    dic.Add("TK", short.Parse(string.Join("", s.TK.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    dic.Add("TF", short.Parse(string.Join("", s.TF.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    dic.Add("Vd1Setting", (short.Parse(string.Join("", s.Vd1Setting.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber) / 10).ToString());
                                    dic.Add("Vd2Setting", (short.Parse(string.Join("", s.Vd2Setting.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber) / 10).ToString());
                                    dic.Add("Vd3Setting", (short.Parse(string.Join("", s.Vd3Setting.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber) / 10).ToString());
                                    dic.Add("TLSetting", (short.Parse(string.Join("", s.TLSetting.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber) / 10.0).ToString("F1"));
                                    dic.Add("THSetting", (short.Parse(string.Join("", s.THSetting.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber) / 10.0).ToString("F1"));
                                    dic.Add("ResistanceTime", (short.Parse(string.Join("", s.ResistanceTime.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber) / 10.0).ToString("F1"));
                                    dic.Add("ResistanceLowerLimit", short.Parse(string.Join("", s.ResistanceLowerLimit.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    dic.Add("Reserve1", Int32.Parse(string.Join("", s.Reserve1.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    dic.Add("Tscale", short.Parse(string.Join("", s.Tscale.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    dic.Add("Vd1", short.Parse(string.Join("", s.Vd1.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    dic.Add("Vd2", short.Parse(string.Join("", s.Vd2.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    dic.Add("Vd3", short.Parse(string.Join("", s.Vd3.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    dic.Add("Vp", short.Parse(string.Join("", s.Vp.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    dic.Add("TP", (short.Parse(string.Join("", s.TP.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber) / 100.0).ToString("F2"));
                                    dic.Add("ResistanceData", (Int32.Parse(string.Join("", s.ResistanceData.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber) / 10.0).ToString("F1"));
                                    dic.Add("Reserve2", short.Parse(string.Join("", s.Reserve2.Select(x => x.ToString("X2"))), System.Globalization.NumberStyles.HexNumber).ToString());
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.NoAddress, short.Parse(dic["No"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.NameAddress, dic["Name"]);
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.VoltageAddress, dic["Voltage"]);
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.TKAddress, short.Parse(dic["TK"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.TFAddress, short.Parse(dic["TF"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.Vd1SettingAddress, short.Parse(dic["Vd1Setting"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.Vd2SettingAddress, short.Parse(dic["Vd2Setting"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.Vd3SettingAddress, short.Parse(dic["Vd3Setting"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.TLSettingAddress, float.Parse(dic["TLSetting"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.THSettingAddress, float.Parse(dic["THSetting"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.ResistanceTimeAddress, float.Parse(dic["ResistanceTime"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.ResistanceLowerLimitAddress, short.Parse(dic["ResistanceLowerLimit"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.TscaleAddress, short.Parse(dic["Tscale"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.Vd1Address, short.Parse(dic["Vd1"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.Vd2Address, short.Parse(dic["Vd2"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.Vd3Address, short.Parse(dic["Vd3"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.VpAddress, short.Parse(dic["Vp"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.TPAddress, float.Parse(dic["TP"]));
                                    APPInstance.PLC.Write(entity.QueryPLCEntity.ResistanceDataAddress, float.Parse(dic["ResistanceData"]));
                                    #endregion
                                    entity.Result = CommonHelper.DictionaryToString(dic, "，");
                                }
                                catch (Exception ex)
                                {
                                    this.Error(ex);
                                }
                            }
                        }
                        result = CommonHelper.GetJsonString(entity, Newtonsoft.Json.Formatting.None);
                        this.Info($"【{pluginParam.Tag}】{methodName} - 结果：{entity.Result}");
                    }
                }
                catch (Exception ex)
                {
                    this.Error(ex);
                }
            }
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}");
            return result;
        }

        /// <summary>
        /// RJ6902GX_TEST 启动测试
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>
        private string RJ6902GXTest(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            string methodName = "Hipot启动测试";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            if (pluginParam != null && APPInstance.PLC != null && APPInstance.PLC.IsConnected)
            {
                var entity = CommonHelper.GetJson(pluginParam.ParamString, new RJ6902ExcuteEntity());
                try
                {
                    if (entity != null && entity.PLCEntity != null)
                    {
                        string testDonetAddress = entity.PLCEntity.TestDoneAddress;
                        entity.IsOK = false;
                        if (this.IsCorrectData(entity.Data))
                        {
                            byte cmd = entity.Data[5];
                            if (cmd == TEST_DATA_RESULT)
                            {
                                byte[] dataBuffer = entity.Data.Skip(6).Take(entity.Data.Length - 8).ToArray();
                                try
                                {
                                    object data = CommonHelper.BytesToStruct(dataBuffer, typeof(StructRJTestJudgeResultData));
                                    if (data != null && data is StructRJTestJudgeResultData)
                                    {
                                        StructRJTestJudgeResultData rtObj = (StructRJTestJudgeResultData)data;
                                        entity.Result = CommonHelper.GetStructString(rtObj);
                                        entity.IsOK = rtObj.Result == 0x01;
                                        #region
                                        Dictionary<string, string> map = CommonHelper.GetStructDic(rtObj);
                                        APPInstance.PLC.Write(entity.PLCEntity.Vd1Address, float.Parse(map["Vd1"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.Vd2Address, float.Parse(map["Vd2"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.Vd3Address, float.Parse(map["Vd3"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.VpAddress, float.Parse(map["Vp"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.UptAddress, float.Parse(map["Upt"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.RtdAddress, float.Parse(map["Rtd"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.CapDataAddress, float.Parse(map["R1"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.OcAddress, short.Parse(map["Oc"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.ScAddress, short.Parse(map["Sc"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.UvAddress, short.Parse(map["Uv"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.OvAddress, short.Parse(map["Ov"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.Drop1Address, short.Parse(map["Drop1"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.Drop2Address, short.Parse(map["Drop2"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.Drop3Address, short.Parse(map["Drop3"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.TLAddress, short.Parse(map["TL"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.THAddress, short.Parse(map["TH"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.RtrAddress, short.Parse(map["Rtr"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.CapResultAddress, short.Parse(map["R2"]));
                                        APPInstance.PLC.Write(entity.PLCEntity.HipotResultAddress, short.Parse(map["Result"]));
                                        #endregion
                                    }
                                }
                                catch (Exception ex)
                                {
                                    this.Error(ex);
                                }
                            }
                        }
                        APPInstance.PLC.Write(testDonetAddress, true);
                        result = CommonHelper.GetJsonString(entity, Newtonsoft.Json.Formatting.None);
                        this.Info($"【{pluginParam.Tag}】{methodName} - 结果：{entity.Result}");
                    }
                }
                catch (Exception ex)
                {
                    this.Error(ex);
                }
            }
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}");
            return result;
        }

        /// <summary>
        /// 是否正确数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private bool IsCorrectData(byte[] data)
        {
            return data != null && data.Length > 4 && data[4] != 0x99;
        }
    }
}
